package cn.itcast.controller;

import cn.itcast.domain.CourseAndSc;
import cn.itcast.domain.Msg;
import cn.itcast.domain.Sc;
import cn.itcast.domain.Student;
import cn.itcast.service.impl.ScServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.JstlUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ScController {

    @Autowired
    private ScServiceImpl scService;

    @Autowired
    Msg msg;
    @Autowired
    Student student;

    @Autowired
    CourseAndSc courseAndSc;


    // 学生选课
    @RequestMapping("/allSc")
    @ResponseBody
    public Msg findByIdToSc(@Param("courseno") Integer courseno, HttpSession session){
        int i = 0;  // 0表示不能   1表示能
        student = (Student)  session.getAttribute("student");
        System.out.println(" 我是课程号 "+courseno);
        if (student != null){
            List<Sc> list = scService.findByIdToSc(student.getSno());
            for (Sc sc:list){
                if (sc.getCourseno().equals(courseno)){
                    System.out.println("失败");
                    msg = Msg.fail().add("have","该课程已选，不能重复选择");
                    i = 0;
                    break;
                }else {
                    System.out.println("成功");
                    i = 1;
                    msg = Msg.success().add("list",list);
                }
            }
            if (i ==  1){
                scService.insertToSc(student.getSno(),courseno);
            }
        }else {
            msg = Msg.fail().add("have","退出了");
        }

       return msg;
    }


    // 学生查询课程
    @RequestMapping("/allCourse")
    @ResponseBody
    public Msg allCourseAndexit(HttpSession session){
        student = (Student)  session.getAttribute("student");
        if (student != null){
            List<CourseAndSc> list = scService.findAllByStuId(student.getSno());
            msg = Msg.success().add("list",list);
        }else {
            msg = Msg.fail().add("have","该课程已选，不能重复选择");
        }

        return msg;
    }

    @RequestMapping("/deleteTocourse")
    @ResponseBody
    // 删除学生选课信息
    public Msg deleteTocourse(@Param("courseno")Integer courseno,HttpSession session){
        student = (Student)  session.getAttribute("student");
        if (student != null){
           scService.deleteToSnoAndCourseno(student.getSno(),courseno);
           System.out.println(courseno);
            msg = Msg.success().add("have","删除成功");
        }else {
            msg = Msg.fail().add("have","该课程已选，不能重复选择");
        }
        return msg;









//        for (CourseAndSc courseAndSc : courseAndScs){
//            if (courseAndSc.getScore()==null){
//                if (student != null){
//                    scService.deleteToSnoAndCourseno(student.getSno(),courseno);
//                    System.out.println(courseno);
//                    msg = Msg.success().add("have","删除成功");
//                }else {
//                    msg = Msg.fail().add("have","该课程已选，不能重复选择");
//                }
//            }else {
//                msg = Msg.fail().add("have","该课程有成绩不能删除");
//            }
//        }
//


    }


}
